﻿using _1._8._2015_SHero_Auth.Data.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _1._8._2015_SHero_Auth.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db)
        {
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            RoleStore<Role> roleStore = new RoleStore<Role>(db);
            RoleManager<Role> roleManaager = new RoleManager<Role>(roleStore);

            if(!roleManaager.RoleExists("General"))
            {
                roleManaager.Create(new Role
                {
                     Name = "General"
                });
            }
            if (!roleManaager.RoleExists("Admin"))
            {
                roleManaager.Create(new Role
                {
                    Name = "Admin"
                });
            }
            ApplicationUser louis = userManager.FindByName("louism817@gmail.com");

            if(louis == null)
            {
                louis = new ApplicationUser
                {
                   FirstName = "Louis",
                   LastName = "Murphy",
                   UserName = "louism817@gmail.com",
                   Email = "louism817@gmail.com"
                };
            
            userManager.Create(louis, "123456");

            louis = userManager.FindByName("louism817@gmail.com");
            }
            else
            {
                if(!userManager.IsInRole(louis.Id,"Admin"))
                {
                    userManager.AddToRole(louis.Id, "Admin");
                }
            }
            db.SaveChanges();
        }
    }
}
